package com.demo.function.shardingsphere;

import com.demo.dao.mapper.UserMapper;
import com.demo.model.entity.db.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 标准分片，用于处理使用单一键作为分片键的 =、IN、BETWEEN AND、>、<、>=、<= 进行分片的场景。
 * 标准分片优先使用精准分片策略，精准分片策略没命中则使用范围分片策略
 * 精准分片策略和范围分片策略只能使用其中一种
 * @author cyj
 */
@Service
public class StandardStrategyService {

    @Resource
    private UserMapper userMapper;

    public void insert(Long userId) {
        User user = new User().setUserId(userId);
        userMapper.insertSelective(user);
    }

    public List<User> queryByUserId(Long userId) {
        return userMapper.queryByUserId(userId);
    }

    public List<User> queryByUserIdGt(Long userId) {
        return userMapper.queryByUserIdGt(userId);
    }

    public List<User> queryByUserIdLt(Long userId) {
        return userMapper.queryByUserIdLt(userId);
    }

    public List<User> queryByUserIdBetween(Long minUserId, Long maxUserId) {
        return userMapper.queryByUserIdBetween(minUserId, maxUserId);
    }

    public List<User> queryByUserIds(Long... userId) {
        return userMapper.queryByUserIds(Arrays.asList(userId));
    }
}
